package com.yycx.module.bus.provider.handler.haiying.common;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.module.export.DictDataVo;
import com.yycx.common.base.module.export.ExportField;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.ExportInterceptor;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.module.admin.provider.mapper.BaseUserCompanyMapper;
import com.yycx.module.bus.client.entity.hy.BusBankStatement;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.provider.service.hy.BusBankStatementService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import com.yycx.module.system.client.entity.SysBank;
import com.yycx.module.system.client.entity.SysCompany;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 我的银行流水
 *
 * @author zyf
 */
@Component
public class HyBankStatementPageListHandler implements PageInterceptor<BusBankStatement>, ExportInterceptor {

    @Autowired
    private BaseUserCompanyMapper baseUserCompanyMapper;

    @Autowired
    private BusBankStatementService bankStatementService;

    @Autowired
    @Lazy
    private BusPaymentService paymentService;

    @Autowired
    @Lazy
    private HyOrderService orderService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery cq, PageParams pageParams, EntityMap params) {
        initCq(cq);
    }

    @Override
    public ResultBody initData(Map params) {
        CriteriaQuery cq = new CriteriaQuery(params, BusBankStatement.class);
        initCq(cq);
        List<EntityMap> list = bankStatementService.selectEntityMap(cq);
        return ResultBody.ok(list);
    }

    /**
     * 初始化查询条件
     *
     * @param cq
     * @return
     */
    private CriteriaQuery initCq(CriteriaQuery cq) {
        OpenUser openUser = OpenHelper.getUser();
        String userName = openUser.getUsername();
        Long userId = openUser.getUserId();
        //查询字段
        cq.select(BusBankStatement.class, "*");
        cq.select(SysBank.class, "bankName");
        cq.select(BusPayment.class, "paymentNo");
        cq.select(SysCompany.class, "companyName");
        cq.eq(BusBankStatement.class, "companyId");
        //查询条件
        cq.eq(BusBankStatement.class, "bankStatementId");
        String amountState = cq.getParams("amountState");
        String beginDate = cq.getParams("beginDate");
        String endDate = cq.getParams("endDate");
        String paymentNo = cq.getParams("paymentNo");
        cq.select(BusBankStatement.class, "*");
        if (FlymeUtils.isNotEmpty(amountState)) {
            cq.eq(true, "amountState", amountState);
        }
        cq.eq(BusBankStatement.class, "takeBankId");
        cq.eq(BusBankStatement.class, "customCode");
        cq.ge(FlymeUtils.isNotEmpty(beginDate), "bbs.amountDate", beginDate);
        cq.le(FlymeUtils.isNotEmpty(endDate), "bbs.amountDate", endDate + " 23:59:59");
        cq.likeRight(FlymeUtils.isNotEmpty(paymentNo), "paymentNo", paymentNo);
        //出纳角色
        Boolean isCn = OpenHelper.hasRole("ROLE_HZ_CN", "ROLE_FLP_CN");
        //出纳看自己上传的
        if (isCn && !userName.contains("admin")) {
            cq.eq(BusBankStatement.class, "userId", OpenHelper.getUserId());
        }
        //只看当前账套数据
        cq.eq("bbs.companyId", OpenHelper.getCompanyId());
        cq.createJoin(SysBank.class).setMainField("takeBankId");
        cq.createJoin(BusPayment.class);
        cq.createJoin(SysCompany.class);
        cq.orderByDesc("bbs.createTime");
        return cq;
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusBankStatement> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap datum : result) {
            Long paymentId = datum.getLong("paymentId");
            if (FlymeUtils.isNotEmpty(paymentId)) {
                BusPayment payment = paymentService.getById(paymentId);
                if (FlymeUtils.isNotEmpty(payment)) {
                    Integer advanceState = FlymeUtils.getInteger(payment.getAdvanceState(), 0);
                    datum.put("paymentNo", payment.getPaymentNo());
                    if (advanceState.equals(0)) {
                        String orderId = payment.getOrderId();
                        HyOrder order = orderService.getById(orderId);
                        if (FlymeUtils.isNotEmpty(order)) {
                            datum.put("paymentNo", order.getOrderNo());
                        }
                    }
                }
            }
        }
        return PageInterceptor.super.complete(cq, result, extra);
    }

    @Override
    public void initExcelExportEntity(ExportField exportField, List list) {
        Object key = exportField.getKey();
        exportField.setWidth(20);
        if (key.equals("amountState")) {
            List<DictDataVo> dictDataVos = new ArrayList<>();
            DictDataVo dictDataVo = new DictDataVo();
            dictDataVo.setLabel("已认款");
            dictDataVo.setValue(1);
            DictDataVo dictDataVo2 = new DictDataVo();
            dictDataVo2.setLabel("未认款");
            dictDataVo2.setValue(0);
            dictDataVos.add(dictDataVo);
            dictDataVos.add(dictDataVo2);
            exportField.setDictData(dictDataVos);
            exportField.setReplace();
        }
    }
}
